package com.inctech.controller.employee;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inctech.flowengine.FlowWebservice;
import com.inctech.interceptor.AuthPassport;
import com.inctech.model.Employee;
import com.inctech.service.IEmployeeService;
import com.inctech.service.IFlowEngineService;

@Controller
@RequestMapping("employee")
public class EmployeeController {

	final Logger logger = LoggerFactory.getLogger(EmployeeController.class);
	
	@Resource(name = "employeeService")
	private IEmployeeService employeeService;
	
	@Resource(name="flowEngineService")
	private IFlowEngineService flow;
	private FlowWebservice getClient() {
		return flow.getFlowClinet();
	}
	
	@RequestMapping("/employee")
	public String employee() {
		return "employee/employee";
	}
	
	@AuthPassport
	@ResponseBody
	@RequestMapping("/getAllEmployee")
	public String getAllEmployee(int page, int rows){
		JSONObject obj = new JSONObject();
		//当为缺省值的时候进行赋值
        int currentpage = ( page == 0) ? 1: page;	//第几页
        int pagesize = (rows == 0) ? 5: rows;	//每页显示数量
        int index = (currentpage - 1) * pagesize;	//从第几条开始显示
        Map<String,Object> paramMap = new HashMap<String, Object>();
        paramMap.put("index", index);
        paramMap.put("pagesize", pagesize);
		try {
			List<Employee> list = employeeService.findAllEmployee(paramMap);
			int total = employeeService.getTocalCount();
			obj.put("total", total);
			obj.put("rows", list);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("出错了", e);
		}
		
		return obj.toString();
	}
	
	@AuthPassport
	@ResponseBody
	@RequestMapping(value="/addEmployee", method=RequestMethod.POST)
	public String addEmployee(@ModelAttribute("employee") Employee employee){
		JSONObject obj = new JSONObject();
		Map<String,Object> paramMap = new HashMap<String, Object>();
		int count = 0;
		paramMap.put("e", employee);
		System.out.println("========="+employee);
		try {
			count = employeeService.addEmployee(paramMap);
			//Workflow增加用户
			JSONObject flowMap = new JSONObject();
			flowMap.put("id", employee.getUsr());
			flowMap.put("name", employee.getName());
			flowMap.put("email", employee.getEmail());
			flowMap.put("pwd", employee.getPwd());
			System.out.println(flowMap.toString());
			this.getClient().createUser(flowMap.toString());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("出错了", e);
		}
		if(count==1)
			obj.put("result", true);
		else
			obj.put("result", false);
		return obj.toString();
	}
	
	@AuthPassport
	@ResponseBody
	@RequestMapping("/updateEmployee")
	public String updateEmployee(@ModelAttribute Employee employee){
		JSONObject obj = new JSONObject();
		Map<String,Object> paramMap = new HashMap<String, Object>();
		int count = 0;
		paramMap.put("e", employee);
		try {
			count = employeeService.updateEmployee(paramMap);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("出错了", e);
		}
		if(count==1)
			obj.put("result", true);
		else
			obj.put("result", false);
		return obj.toString();
	}
	
	@AuthPassport
	@ResponseBody
	@RequestMapping("/deleteEmployee")
	public String deleteEmployee(int id){
		JSONObject obj = new JSONObject();
		int count = 0;
		try {
			count = employeeService.deleteEmployee(id);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("出错了", e);
		}
		if(count==1)
			obj.put("result", true);
		else
			obj.put("result", false);
		return obj.toString();
	}
	
	@AuthPassport
	@ResponseBody
	@RequestMapping("/getAllDepartment")
	public String getAllDepartment(){
		JSONObject obj = new JSONObject();
		JSONArray array = new JSONArray();
		try {
			List<String> list = employeeService.getAllDepartment();
			for (int i = 0; i < list.size(); i++) {
				obj.put("id", i+1);
				obj.put("text", list.get(i));
				array.add(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("出错了", e);
		}
		return array.toString();
	}
	
	@AuthPassport
	@ResponseBody
	@RequestMapping("/getPositionByDep")
	public String getPositionByDep(String department){
		JSONObject obj = new JSONObject();
		JSONArray array = new JSONArray();
		try {
			List<String> list = employeeService.getPositionByDep(department);
			for (int i = 0; i < list.size(); i++) {
				obj.put("id", i+1);
				obj.put("text", list.get(i));
				array.add(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("出错了", e);
		}
		return array.toString();
	}
	
	@AuthPassport
	@ResponseBody
	@RequestMapping("/updateEmployeeJob")
	public String updateEmployeeJob(int id,String department,String position){
		JSONObject obj = new JSONObject();
		int count = 0;
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("id", id);
			paramMap.put("department", department);
			paramMap.put("position", position);
			count = employeeService.updateEmployeeJob(paramMap);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("出错了", e);
		}
		if(count==1){
			obj.put("result", true);			
		}else{			
			obj.put("result", false);
		}		
		return obj.toString();
	}
}
